package com.shuqi.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.model.manager.MyAccountManager;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.RechargesHistoryParser;

/**
 * 充值记录model
 * 
 * @author zhuchen
 * 
 */
public class RechargesModel extends ModelBase implements HttpRequestListener {

    private HttpRequest httpRequest;
    private Context context;
    private String pageIndex;

    public RechargesModel(Context context) {
        this.context = context;
    }

    @Override
    public void loadData(Object... objs) {
        this.pageIndex = (String) objs[0];
        this.httpRequest =
                new HttpRequest(context, UrlManager.COLUMN_DOMAIN_PAY, getRequestUrl(), setParams(), this);
        this.httpRequest.setDataParseAdapter(new RechargesHistoryParser());
        MyTask.runInBackground(httpRequest, true);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        if (httpRequest != null) {
            httpRequest.abort();
        }
    }

    @Override
    public void action(int actionCode, Object object) {
        // TODO Auto-generated method stub
        fireEvent(actionCode, object);
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        // TODO Auto-generated method stub
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        long timestamp = System.currentTimeMillis();
        String key = "4d30ef50cbd32ed92deb6fbd2321e951";// 充值记录
        String strTimestamp = timestamp + "";
        String md5_key_sb = key + timestamp;
        String md5_key = com.shuqi.common.utils.Util.MD5(md5_key_sb.toString());
        params.add(new BasicNameValuePair("key", md5_key));// md5
        params.add(new BasicNameValuePair("timestamp", strTimestamp));// 时间戳
        params.add(new BasicNameValuePair("userid", MyAccountManager.getInstance(context)
                .getUserId()));
        return params;

    }

    @Override
    public String getRequestUrl() {
        // TODO Auto-generated method stub
        return Urls.getPDHistory(true, this.pageIndex);
    }

}
